<template>
    <div>
        <el-row :gutter="20">
            <el-col :span="8">
                <el-card shadow="hover" class="mgb20" style="height: 252px">
                    <div class="user-info">
                        <img src="../../../assets/img/employee_info.jpeg" class="user-avator" alt />
                        <div class="user-info-cont">
                            <div class="user-info-name">{{ name }}</div>
                            <div style="font-size: 20px; font-weight: 600; color: green">{{ role }}</div>
                        </div>
                    </div>
                    <div class="user-info-list">
                        当前时间：<span style="font-size: 20px; font-weight: 500; color: black">{{ date | formatDate }}</span>
                    </div>
                    <div class="user-info-list">
                        当前登录地点：
                        <span style="font-size: 20px; font-weight: 500; color: black">{{ LocationCity }}</span>
                    </div>
                </el-card>
                <el-card shadow="hover" style="height: 550px">
                    <div slot="header" class="clearfix">
                        <span style="font-weight:600;">入站快递统计(实时)</span>
                    </div>
                    京东 <el-progress :percentage="JD" color="#42b983" style="margin-bottom:10px;"></el-progress> 顺丰
                    <el-progress :percentage="SF" color="#f1e05a" style="margin-bottom:10px;"></el-progress> 中通
                    <el-progress :percentage="ZT" color="red" style="margin-bottom:10px;"></el-progress> 圆通
                    <el-progress :percentage="YT" color="blue" style="margin-bottom:10px;"></el-progress> 申通
                    <el-progress :percentage="ST" color="black" style="margin-bottom:10px;"></el-progress> 韵达
                    <el-progress :percentage="YD" color="cyan" style="margin-bottom:10px;"></el-progress> 天天
                    <el-progress :percentage="TT" color="yellow" style="margin-bottom:10px;"></el-progress> EMS
                    <el-progress :percentage="EMS" color="orange"></el-progress>
                </el-card>
            </el-col>
            <el-col :span="16">
                <el-row :gutter="20" class="mgb20">
                    <el-col :span="6">
                        <el-card shadow="hover" :body-style="{ padding: '0px' }">
                            <div class="grid-content grid-con-1">
                                <i class="el-icon-present grid-con-icon"></i>
                                <div class="grid-cont-right">
                                    <div class="grid-num">{{ totalPackages }}</div>
                                    <div>全部包裹</div>
                                </div>
                            </div>
                        </el-card>
                    </el-col>
                    <el-col :span="6">
                        <el-card shadow="hover" :body-style="{ padding: '0px' }">
                            <div class="grid-content grid-con-2">
                                <i class="el-icon-price-tag grid-con-icon"></i>
                                <div class="grid-cont-right">
                                    <div class="grid-num">{{ waitingPackages }}</div>
                                    <div>待入库</div>
                                </div>
                            </div>
                        </el-card>
                    </el-col>
                    <el-col :span="6">
                        <el-card shadow="hover" :body-style="{ padding: '0px' }">
                            <div class="grid-content grid-con-3">
                                <i class="el-icon-discount grid-con-icon"></i>
                                <div class="grid-cont-right">
                                    <div class="grid-num">{{ exportedPackages }}</div>
                                    <div>已出库</div>
                                </div>
                            </div>
                        </el-card>
                    </el-col>
                    <el-col :span="6">
                        <el-card shadow="hover" :body-style="{ padding: '0px' }">
                            <div class="grid-content grid-con-4">
                                <i class="el-icon-chat-line-square grid-con-icon"></i>
                                <div class="grid-cont-right">
                                    <div class="grid-num">{{ appointmentPackages }}</div>
                                    <div>预约上门</div>
                                </div>
                            </div>
                        </el-card>
                    </el-col>
                </el-row>
                <el-card shadow="hover" style="height: 700px">
                    <div slot="header" class="clearfix">
                        <el-calendar v-model="value"> </el-calendar>
                    </div>
                </el-card>
            </el-col>
        </el-row>
      
    </div>
</template>

<script>
import Schart from 'vue-schart';
import MapLoader from '../../../AMap.js';
var padDate = function (value) {
    // console.log(value);
    return value < 10 ? '0' + value : value;
};
export default {
    name: 'dashboard',
    data() {
        return {
            JD: '',
            SF: '',
            YT: '',
            ZT: '',
            ST: '',
            YD: '',
            TT: '',
            EMS: '',
            totalPackages: 0,
            waitingPackages: 0,
            exportedPackages: 0,
            appointmentPackages: 0,
            LocationCity: '',
            value: new Date(),
            date: new Date(),
            role: '快递员',
            name: localStorage.getItem('user_name'),

            data: [
                {
                    name: '2018/09/04',
                    value: 1083
                },
                {
                    name: '2018/09/05',
                    value: 941
                },
                {
                    name: '2018/09/06',
                    value: 1139
                },
                {
                    name: '2018/09/07',
                    value: 816
                },
                {
                    name: '2018/09/08',
                    value: 327
                },
                {
                    name: '2018/09/09',
                    value: 228
                },
                {
                    name: '2018/09/10',
                    value: 1065
                }
            ],
            options: {
                type: 'bar',
                title: {
                    text: '最近一周各品类销售图'
                },
                xRorate: 25,
                labels: ['周一', '周二', '周三', '周四', '周五'],
                datasets: [
                    {
                        label: '家电',
                        data: [234, 278, 270, 190, 230]
                    },
                    {
                        label: '百货',
                        data: [164, 178, 190, 135, 160]
                    },
                    {
                        label: '食品',
                        data: [144, 198, 150, 235, 120]
                    }
                ]
            },
            options2: {
                type: 'line',
                title: {
                    text: '最近几个月各品类销售趋势图'
                },
                labels: ['6月', '7月', '8月', '9月', '10月'],
                datasets: [
                    {
                        label: '家电',
                        data: [234, 278, 270, 190, 230]
                    },
                    {
                        label: '百货',
                        data: [164, 178, 150, 135, 160]
                    },
                    {
                        label: '食品',
                        data: [74, 118, 200, 235, 90]
                    }
                ]
            }
        };
    },
    components: {
        Schart
    },
    computed: {
        role() {
            return this.name === 'admin' ? '超级管理员' : '普通用户';
        }
    },
    created() {
        console.log(localStorage.getItem('user_name'));
        console.log(localStorage.getItem('user_id'));
        MapLoader().then(
            (AMap) => {
                AMap.plugin('AMap.CitySearch', () => {
                    const citysearch = new AMap.CitySearch(); // 自动获取用户IP，返回当前城市
                    citysearch.getLocalCity((status, result) => {
                        if (status === 'complete' && result.info === 'OK') {
                            if (result && result.city && result.bounds) {
                                this.LocationCity = result.city;
                                console.log('当前城市', this.LocationCity); // var citybounds = result.bounds; // 地图显示当前城市 // map.setBounds(citybounds);
                            }
                        } else {
                            console.log('定位出错');
                        }
                    });
                });
            },
            (err) => {
                console.log('地图加载失败', err);
            }
        );

        const _this = this;
        this.$axios({
            method: 'GET',
            url: 'statistics/statistics_packages_by_staff'
        })
            .then(function (res) {
                if (res.data.code == 1) {
                    _this.totalPackages = res.data.totalPackages;
                    _this.waitingPackages = res.data.waitingPackages;
                    _this.exportedPackages = res.data.exportedPackages;
                    _this.appointmentPackages = res.data.appointmentPackages;
                }
            })
            .catch(function (err) {
                console.log(err);
            });


        this.$axios({
            method: 'GET',
            url: 'statistics/statistics_express'
        })
            .then(function (res) {
                if (res.data.code == 1) {
                    _this.JD = (parseFloat(res.data.JD)*100 ).toFixed(1);
                    _this.SF = (parseFloat(res.data.SF)*100 ).toFixed(1);
                    _this.YT = (parseFloat(res.data.YT)*100 ).toFixed(1);
                    _this.ZT = (parseFloat(res.data.ZT)*100 ).toFixed(1);
                    _this.ST = (parseFloat(res.data.ST)*100 ).toFixed(1);
                    _this.YD = (parseFloat(res.data.YD)*100 ).toFixed(1);
                    _this.TT = (parseFloat(res.data.TT)*100 ).toFixed(1);
                    _this.EMS = (parseFloat(res.data.EMS)*100 ).toFixed(1);
                }
            })
            .catch(function (err) {
                console.log(err);
            });
    },

    methods: {
        changeDate() {
            const now = new Date().getTime();
            this.data.forEach((item, index) => {
                const date = new Date(now - (6 - index) * 86400000);
                item.name = `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`;
            });
        }
    },
    filters: {
        formatDate: function (value) {
            var date = new Date(value);
            console.log(date.getDate());
            var year = date.getFullYear();
            var month = padDate(date.getMonth() + 1);
            var day = padDate(date.getDate());
            var hours = padDate(date.getHours());
            var minutes = padDate(date.getMinutes());
            var seconds = padDate(date.getSeconds());
            // 整理并返回
            return year + '年' + month + '月' + day + '日' + ' ' + hours + ':' + minutes + ':' + seconds;
        }
    },
    mounted() {
        let _this = this; // 声明一个变量指向Vue实例this，保证作用域一致
        this.timer = setInterval(() => {
            _this.date = new Date(); // 修改数据date
        }, 1000);
    },
    beforeDestroy() {
        if (this.timer) {
            clearInterval(this.timer); // 在Vue实例销毁前，清除我们的定时器
        }
    }
};
</script>


<style scoped>
.el-row {
    margin-bottom: 20px;
}

.grid-content {
    display: flex;
    align-items: center;
    height: 100px;
}

.grid-cont-right {
    flex: 1;
    text-align: center;
    font-size: 14px;
    color: #999;
}

.grid-num {
    font-size: 30px;
    font-weight: bold;
}

.grid-con-icon {
    font-size: 50px;
    width: 100px;
    height: 100px;
    text-align: center;
    line-height: 100px;
    color: #fff;
}

.grid-con-1 .grid-con-icon {
    background: rgb(45, 140, 240);
}

.grid-con-1 .grid-num {
    color: rgb(45, 140, 240);
}

.grid-con-2 .grid-con-icon {
    background: rgb(100, 213, 114);
}

.grid-con-2 .grid-num {
    color: rgb(45, 140, 240);
}

.grid-con-3 .grid-con-icon {
    background: rgb(242, 94, 67);
}

.grid-con-3 .grid-num {
    color: rgb(242, 94, 67);
}
.grid-con-4 .grid-con-icon {
    background: rgb(242, 9, 237);
}
.grid-con-4 .grid-num {
    color: rgb(242, 9, 237);
}
.user-info {
    display: flex;
    align-items: center;
    padding-bottom: 20px;
    border-bottom: 2px solid #ccc;
    margin-bottom: 20px;
}

.user-avator {
    width: 120px;
    height: 120px;
    border-radius: 50%;
}

.user-info-cont {
    padding-left: 50px;
    flex: 1;
    font-size: 14px;
    color: #999;
}

.user-info-cont div:first-child {
    font-size: 30px;
    color: #222;
}

.user-info-list {
    font-size: 14px;
    color: #999;
    line-height: 25px;
    font-weight: 500;
    color: black;
}

.user-info-list span {
    margin-left: 70px;
}

.mgb20 {
    margin-bottom: 20px;
}

.todo-item {
    font-size: 14px;
}

.todo-item-del {
    text-decoration: line-through;
    color: #999;
}

.schart {
    width: 100%;
    height: 300px;
}
</style>
